package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dao.CourseDataMapper;
import com.zhihuishu.micro.course.dto.CourseDataDto;
import com.zhihuishu.micro.course.dto.CourseFolderDto;
import com.zhihuishu.micro.course.model.CourseData;
import com.zhihuishu.micro.course.service.CCDataTreenityDBService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * table-DATA
 *
 * @author Jiangli
 * @date 2016/12/16 13:15
 */
@Service("ccDataTreenityServiceDB")
public class CCDataTreenityDBServiceImpl extends BaseTreenityBatchServiceDBImpl<CourseDataDto, CourseData> implements CCDataTreenityDBService {
    @Resource
    private CourseDataMapper courseDataMapper;

    @Override
    public CourseDataDto updateAndGet(CourseDataDto dto, long updateUserId) {
        //无需覆盖
        return null;
    }

    @Override
    public CourseDataDto refreshUniqueName(CourseDataDto dto, long updateUserId) {
        //无需覆盖
        return null;
    }

    @Override
    public List<String> findFileNameForCreate(Long folderId, String likePattern) {
        return courseDataMapper.findFileNameForCreate(folderId, likePattern);
    }

    @Override
    public List<String> findFileNameForUpdate(Long dataId, String likePattern) {
        return courseDataMapper.findFileNameForUpdate(dataId, likePattern);
    }

    @Override
    public CourseData toModel(CourseDataDto dto) {
        if (dto == null) return null;
        CourseData model = new CourseData();
        BeanHelper.copyProperties(dto, model);
        return model;
    }

    @Override
    public CourseDataDto toDto(CourseData model) {
        if (model == null) return null;
        CourseDataDto dto = new CourseDataDto();
        BeanHelper.copyProperties(model, dto);
        return dto;
    }

    @Override
    public Integer create(CourseDataDto dto, long creator) {
        dto.setUserId(creator);
        CourseData model = toModel(dto);

        courseDataMapper.saveCourseData(model);

        Integer dtoId = model != null ? model.getId() : null;
        dto.setId(dtoId);

        return dtoId;
    }


    @Override
    public CourseDataDto detail(Integer dtoId) {
        CourseData detail = courseDataMapper.selectCourseData(dtoId);
        return toDto(detail);
    }

    @Override
    public List<Integer> listDtoIds(long folderId) {
        return courseDataMapper.listIdsByFolderId(folderId);
    }

    @Override
    public List<CourseDataDto> list(List<Integer> listDtoIds) {
        final List<CourseDataDto> ret = new ArrayList<CourseDataDto>();

        if (!CollectionUtils.isEmpty(listDtoIds)) {
            List<CourseData> list = courseDataMapper.selectCourseDatas(new HashSet<Integer>(listDtoIds));
            for (CourseData model : list) {
                ret.add(toDto(model));
            }
        }
        return ret;
    }

    @Override
    public void update(CourseDataDto dto, long updateUserId) {
        CourseData model = toModel(dto);
        courseDataMapper.update(model);
    }

    @Override
    public void delete(Integer dtoId, long deleteUserId) {
        courseDataMapper.deleteCourseData(dtoId);
    }

    @Override
    public void deleteMulti(List<Integer> dtoIds, long deleteUserId) {
        courseDataMapper.deleteMulti(dtoIds);
    }

    @Override
    public void sort(List<CourseDataDto> courseDataDtos, long updateUserId) {

    }

    @Override
    public List<CourseDataDto> listByFolderIdsFilterDataType(List<Long> folderIds, String dataType) {
        final List<CourseDataDto> ret = new ArrayList<CourseDataDto>();

        if (!CollectionUtils.isEmpty(folderIds)) {
            List<CourseData> list = courseDataMapper.selectFilterDatatype(folderIds,dataType);

            for (CourseData model : list) {
                ret.add(toDto(model));
            }
        }
        return ret;
    }

    @Override
    public void updateMulti(List<CourseDataDto> dtos, long updateUserId) {
        List<Integer> idList = new ArrayList<Integer>(dtos.size());
        Long folderId = null;
        Integer privilege = null;
        for (CourseDataDto dto : dtos) {
            idList.add(dto.getId());

            folderId = dto.getFolderId();
            privilege = dto.getPrivilege();
        }

        courseDataMapper.updateMulti(idList,folderId,privilege);
    }
}
